import serial
import time
import subprocess
#shellret="sudo hwclock -r"
shellret="date"
ser=serial.Serial("/dev/ttyAMA0",115200)
def main():
    while True:
        count=ser.inWaiting()
        (ret,data)=subprocess.getstatusoutput(shellret)
        if count!=0:
            recv=ser.read(count)
            try:
               recv=recv.decode('gbk')
            except UnicodeDecodeError:
               print("解码错误")
               main()
            print(data,recv)
            ser.flushInput()
            time.sleep(0.1)
if __name__=='__main__':
    try:
        main()
    except Exception:
        print("发生异常")
        main()
    except IOError:
        print("读取文件失败")
        main()
    except KeyboardInterrupt:
        print("用户退出")
        if ser!=None:
            ser.close()
